# models.py
from extensions import db, login_manager
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime, timezone, timedelta
from config import Config

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))
    bio = db.Column(db.Text)
    avatar = db.Column(db.String(100))  # 头像路径
    gender = db.Column(db.String(10))
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(Config.TIMEZONE))
    is_admin = db.Column(db.Boolean, default=False)
    photos = db.relationship('Photo', backref='author', lazy='dynamic')
    votes = db.relationship('Vote', backref='voter', lazy='dynamic')
    user_favorites = db.relationship('Favorite', backref='user', lazy='dynamic')
    # 使用字符串引用避免循环依赖
    sent_messages = db.relationship('Message', foreign_keys='Message.sender_id', backref='sender', lazy='dynamic')
    received_messages = db.relationship('Message', foreign_keys='Message.receiver_id', backref='receiver', lazy='dynamic')

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def is_favorite(self, photo):
        from models import Favorite  # 延迟导入避免循环依赖
        return Favorite.query.filter_by(user_id=self.id, photo_id=photo.id).first() is not None

class Photo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    filename = db.Column(db.String(100), nullable=False)
    thumbnail = db.Column(db.String(100))
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(Config.TIMEZONE))
    vote_count = db.Column(db.Integer, default=0)
    approved = db.Column(db.Boolean, default=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    category = db.Column(db.String(50))
    photo_votes = db.relationship('Vote', backref='photo', lazy='dynamic')
    photo_comments = db.relationship('Comment', backref='photo', lazy='dynamic')
    photo_favorites = db.relationship('Favorite', backref='photo', lazy='dynamic')

class Vote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    photo_id = db.Column(db.Integer, db.ForeignKey('photo.id'))
    timestamp = db.Column(db.DateTime, default=lambda: datetime.now(Config.TIMEZONE))

class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    sender_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    receiver_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    read = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(Config.TIMEZONE))

class Favorite(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    photo_id = db.Column(db.Integer, db.ForeignKey('photo.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(Config.TIMEZONE))

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(Config.TIMEZONE))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    photo_id = db.Column(db.Integer, db.ForeignKey('photo.id'))
    user = db.relationship('User', backref='comments', lazy='selectin')